package com.step;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.opengl.GLSurfaceView;

import com.step.io.Files;

public abstract class GameActivity extends Activity{
	protected GLSurfaceView glSurfaceView;
	protected IGameView renderer;
	
	/**
	 * 初始化应用。如果没有调用该方法，则游戏屏幕不会更新。
	 * @return
	 */
	public void initialize(IGameView renderer){
		this.renderer = renderer;
		// =============== 下面几行很重要====================================
		Step.context = this;
		GameRenderer graphics = new GameRenderer(renderer);
		Step.renderer = graphics;
		Step.files = new Files(getAssets());
		// ================================================================
		glSurfaceView = new GLSurfaceView(this);
		glEGLConfig();
		glSurfaceView.setRenderer(graphics);
		glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);// 主动刷新
		onLayoutContentView();		
	}

	/**
	 * 自定义GL配置，例如：
	 * GL背景透明，除了设置整个场景背景色为0外，需要进行如下设置
	 * glSurfaceView.setEGLConfigChooser(8,8,8,8, 16, 0);
	 * glSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
	 * GL日志选项配置，
	 * glSurfaceView.setDebugFlags(GLSurfaceView.DEBUG_CHECK_GL_ERROR | GLSurfaceView.DEBUG_LOG_GL_CALLS);
	 */
	protected void glEGLConfig() {
		glSurfaceView.setEGLConfigChooser(true);
		glSurfaceView.getHolder().setFormat(PixelFormat.RGBA_8888);
		glSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
	}

	/**
	 * 自定义布局，子类重写该方法。
	 */
	protected void onLayoutContentView() {
		setContentView(glSurfaceView);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		glSurfaceView.onResume();
		renderer.resume();
	}
	
	@Override
	protected void onPause(){
		super.onPause();
		glSurfaceView.onPause();
		renderer.pause();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		renderer.dispose();
	}

	/**
	 * 设置游戏刷新方式
	 * @param continuously 值为true,则持续刷新;默认为持续刷新。
	 */
	public void setRenderMode(boolean continuously){
		if (continuously){
    		glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
    	}
		else{
    		glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    	}
    }
}
